<template>
	<view class="content">
		<view class="check_name">
			<view style="text-align: center;">
				<button class="my_txImg1" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" plain="true">
					<!-- <image class="my_txImg" :src="userInfo.userImg"></image> -->
					<image style="width: 108rpx;height: 108rpx;" :src="userInfo.avatar" mode=""></image>
				</button>
			</view>
			<view style="padding: 50rpx 20rpx 20rpx 20rpx;" class="usersetup">
				<view class="userbox">
					<text class="user">用户id</text><text class="users">{{userInfo.id}}</text>
				</view>
				<view class="userbox">
					<text class="user">用户姓名</text><text class="users">{{userInfo.name}}</text>
				</view>
				<view class="userbox">
					<text class="user">卡号</text><text class="users">{{userInfo.cardId}}</text>
				</view>
				<view class="userbox">
					<text class="user">手机号</text><text class="users">{{userInfo.mobile}}</text>
				</view>
				<view class="userbox">
					<text class="user">性别</text>
					<view>
						<u-radio-group v-model="radiovalue1" placement="row" @change="groupChange">
							<u-radio activeColor='#5CAF5D' :customStyle="{marginRight: '8px'}"
								v-for="(item, index) in radiolist1" :key="index" :label="item.name" :name="item.name">
							</u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="userbox">
					<text class="user">生日</text>
					<view class="">
						<u-input placeholder='生日一经修改,就不可再更改'></u-input>
					</view>
				</view>
				<view
					style="display: flex;justify-content: space-between;width: 100%;height: 124rpx;align-items: center;"
					@click="modelShow = true">
					<text>支付密码</text>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<u-button :customStyle="customStyles" type='success' shape='circle' @click="submit('baocun')">保存</u-button>
		<u-button :customStyle="customStyless" type='default' shape='circle' @click="submit('tuichu')">退出登录</u-button>
		<u-modal :show="modelShow" :title="title" :showConfirmButton='false'>
			<view class="slot-content">
				<view style="position: absolute; top: 30rpx;right: 56rpx;color: #828282;font-weight: bolder;"
					@click="modelShow=false">
					X
				</view>
				<view style="display: flex;justify-content: space-between;margin-bottom: 30rpx;align-items: center;">
					<text style="margin-right: 46rpx;">新密码</text> <u-input placeholder='输入新密码'></u-input>
				</view>
				<view style="display: flex;justify-content: space-between;align-items: center;">
					<text style="margin-right: 20rpx;">确认密码</text> <u-input placeholder='请确认密码'></u-input>
				</view>
				<view class="confirmButton" style="margin-top: 40rpx;">
					<u-button type='success' shape='circle'>确认</u-button>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import user from "@/api/user.js"
	export default {
		data() {
			return {
				imgSrc: "../../static/img/userheader.png",
				userInfo: '', //用户信息
				radiolist1: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					},
				],
				modelShow: false,
				title: '修改密码',
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				radiovalue1: '男',
				customStyles: {
					width: '634rpx',
					marginTop: '50rpx'
				},
				customStyless: {
					width: '634rpx',
					marginTop: '30rpx',
					background: '#E4E4E4'
				},
				customStyleModel: {
					width: ''
				}
			};
		},
		onShow() {
			const data = uni.getStorageSync("data");
			if (data) {
				this.userInfo = data.data.userInfo;
				console.log(this.userInfo);
			}
			this.getUser()
		},
		methods: {
			submit(val) {
				if (val == 'baocun') {
					console.log('baocun');
				} else if (val == 'tuichu') {
					console.log('tuichu');
				}
			},
			groupChange(val) {
				console.log('groupChange', val);
			},
			//修改头像
			onChooseAvatar(e) {
				console.log('e.detail', e.detail);
				this.imgSrc = e.detail.avatarUrl;
				this.uploadFile(this.imgSrc);
			},
			//查询用户信息
			getUser(){
				user.findById(this.userInfo.id).then(res=>{
					console.log(res);
					if(res.code == 0){
						this.userInfo = res.data
					}
				})
			},
			uploadFile(filePath) {
				console.log('上传图片', filePath);
				uni.uploadFile({
					url: 'http://192.168.1.111:48081/app-api/member/user/update-avatar', // 服务器地址
					filePath: filePath,
					name: 'avatarFile',
					header: {
						'Content-Type': 'multipart/form-data',
						'tenant-id': 1,
						'Authorization': 'Bearer ' + uni.getStorageSync('data').data.accessToken
					},
					success: (upload) => {
						try {
							let json = JSON.parse(upload.data);
							console.log('json', json);
						} catch (error) {
							console.error('解析响应失败:', error);
						}
					},
					fail: (e) => {
						console.error('上传失败:', e);
						uni.$u.toast('上传失败!');
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.content {
		overflow: hidden;
	}

	.my_txImg1 {
		width: 108rpx;
		height: 108rpx;
		border-radius: 50%;
		position: absolute;
		top: 10rpx;
		left: 43%;
		border: 1rpx solid white;

		image {
			position: absolute;
			right: -2rpx;
		}

	}

	.check_name {
		margin-top: 70rpx;
	}

	.usersetup {
		width: 610rpx;
		height: 894rpx;
		margin: auto;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 12rpx 2rpx rgba(0, 0, 0, 0.05);
		border-radius: 20rpx 20rpx 20rpx 20rpx;

		.userbox {
			width: 100%;
			height: 124rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx dashed #E4E4E4;

			.user {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 28rpx;
				color: #333333;
			}

			.users {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #B2B2B2;
			}
		}
	}
</style>